<?php
	class PageNotFoundException extends Exception { public $paegid; }
	
	class Page {
		public static function Display($arg, $single = true) {
			if(is_array($arg)) {
				
				$arguments = array(
					'page.title' => $arg['title'],
					'page.content' => $arg['content'],
					'page.date' => date('jS F Y', $arg['time']),
					'page.time' => date('g:ia', $arg['time']),
					'page.url' => Page::GetURL($arg['pageid'])
				);
				
				$user = false;
				try {
					$user = User::GetInfoById($arg['userid']);
				}
				catch(UserNotFoundException $ex) {
					$user = array(
						'userid' => 0,
						'username' => '',
						'real_name' => 'Deleted author',
						'location' => '',
						'bio' => '',
						'url' => User::GetURL(0)
					);
				}
				$arguments['user.userid'] = $user['userid'];
				$arguments['user.username'] = $user['username'];
				$arguments['user.real_name'] = $user['real_name'];
				$arguments['user.location'] = $user['location'];
				$arguments['user.bio'] = $user['bio'];
				$arguments['user.url'] = User::GetURL($user['userid']);
				
				echo HTML::ParseTemplate('page', $arguments);
			}
			else {
				self::Display(self::Get($arg));
			}
		}
		
		public static function GetURL($pageid) {
			if(!Settings::Get('pretty_link')) {
				return (Settings::Get('blog_url') . '/page.php?id=' . $pageid);
			}
			else {
				return Utilities::CreatePrettyLink('page', $pageid);
			}
		}
		
		public static function Modify($pageid, $title, $content, $show_in_menu) {
			$result = DB::Query('UPDATE `' . fn('pages') . '` SET `title` = \'' . DB::Escape($title) . '\', `content` = \'' . DB::Escape($content) . '\', `show_in_menu` = \'' . (($show_in_menu) ? '1' : '0') . '\' WHERE `pageid` = \'' . DB::Escape($pageid) . '\' LIMIT 1');
			
			return $result;
		}
		
		public static function Exists($title) {
			$result = DB::Query('SELECT * FROM `' . fn('pages') . '` WHERE `title` = \'' . DB::Escape($title) . '\'');
			
			return ($result->num_rows != 0);
		}
		
		public static function Add($title, $content, $show_in_menu) {
			$result = DB::Query('INSERT INTO `' . fn('pages') . '` (`userid`, `title`, `content`, `time`, `show_in_menu`) VALUES(\'' . User::Info('userid') . '\', \'' . DB::Escape($title) . '\', \'' . DB::Escape($content) . '\', \'' . time() . '\', \'' . (($show_in_menu) ? '1' : '0') . '\')');
			
			return $result;
		}
		
		public static function Delete($pageid) {
			$result = DB::Query('DELETE FROM `' . fn('pages') . '` WHERE `pageid` = \'' . DB::Escape($pageid) . '\' LIMIT 1');
			
			return $result;
		}
		
		public static function GetAllPages($show_hidden_from_menu = false, $count = false) {
			if($count) {
				$result = DB::Query('SELECT COUNT(*) FROM `' . fn('pages') . '` ' . (($show_hidden_from_menu) ? '' : 'WHERE `show_in_menu` = \'1\''));
				$row = $result->fetch_assoc();
				return $row['COUNT(*)'];
			}
			
			$result = DB::Query('SELECT * FROM `' . fn('pages') . '` ' . (($show_hidden_from_menu) ? '' : 'WHERE `show_in_menu` = \'1\'') . ' ORDER BY `time` DESC');
			
			return DB::GetAllRows($result);
		}
		
		public static function Get($pageid) {
			$result = DB::Query('SELECT * FROM `' . fn('pages') . '` WHERE `pageid` = \'' . DB::Escape($pageid) . '\' LIMIT 1');
			
			if($result->num_rows == 0) {
				$ex = new PageNotFoundException();
				$ex->pageid = $pageid;
				throw $ex;
			}
			
			return $result->fetch_assoc();
		}
		
		public static function GetPagesByUser($userid, $start = 0, $count = 10) {
			if($start == -1) {
				$result = DB::Query('SELECT COUNT(*) FROM `' . fn('pages') . '` WHERE `userid` = \'' . DB::Escape($userid) . '\'');
				$row = $result->fetch_assoc();
				return $row['COUNT(*)'];
			}
			
			$result = DB::Query('SELECT * FROM `' . fn('pages') . '` WHERE `userid` = \'' . $userid . '\' LIMIT ' . $start . ',' . $count);
			
			return DB::GetAllRows($result);
		}
	}
?>